package com.aizu.listener;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.aizu.common.Constant;
import com.aizu.service.CommonService;
import com.isp.utils.JsonUtil;

@Component
public class StartupListener implements ServletContextAware {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private CommonService commonService;

	@Override
	public void setServletContext(ServletContext servletContext) {
		try {
			List<Map<String, Object>> allAreaList = commonService.findAllArea();
			servletContext.setAttribute(Constant.All_AREA_LIST, allAreaList);
			servletContext.setAttribute(Constant.All_AREA_LIST_MAP, list2Map(allAreaList));
			// servletContext.setAttribute(Constant.All_AREA_LIST_JSON,
			// JsonUtil.toJson(areaList));
			log.info("load allAreaList count " + allAreaList.size() + " size");

			List<Map<String, Object>> cityList = findCity(allAreaList);
			servletContext.setAttribute(Constant.CITY_LIST, cityList);
			servletContext.setAttribute(Constant.CITY_LIST_MAP, list2Map(cityList));
			servletContext.setAttribute(Constant.CITY_LIST_JSON, JsonUtil.toJson(cityList));
			log.info("find provinceList in allAreaList, count " + cityList.size() + " size");

			List<Map<String, Object>> areaList = findArea(allAreaList);
			servletContext.setAttribute(Constant.AREA_LIST, areaList);
			servletContext.setAttribute(Constant.AREA_LIST_MAP, list2Map(areaList));
			servletContext.setAttribute(Constant.AREA_LIST_JSON, JsonUtil.toJson(areaList));
			log.info("find area in allAreaList, count " + areaList.size() + " size");

			List<Map<String, Object>> streetList = commonService.findStreet();
			servletContext.setAttribute(Constant.STREET_LIST, streetList);
			servletContext.setAttribute(Constant.STREET_MAP, list2Map(streetList));
			servletContext.setAttribute(Constant.STREET_LIST_JSON, JsonUtil.toJson(streetList));
			log.info("load streetList count " + streetList.size() + " size");

			List<Map<String, Object>> departmentList = commonService.findDepartment();
			servletContext.setAttribute(Constant.DEPARTMENT_LIST, departmentList);
			servletContext.setAttribute(Constant.DEPARTMENT_MAP, list2Map(departmentList));
			servletContext.setAttribute(Constant.DEPARTMENT_LIST_JSON, JsonUtil.toJson(departmentList));
			log.info("load departmentList count " + departmentList.size() + " size");

			List<Map<String, Object>> communityList = commonService.findCommunity();
			servletContext.setAttribute(Constant.COMMUNITY_LIST, communityList);
			servletContext.setAttribute(Constant.COMMUNITY_MAP, list2Map(communityList));
			servletContext.setAttribute(Constant.COMMUNITY_LIST_JSON, JsonUtil.toJson(communityList));
			log.info("load communityList count " + communityList.size() + " size");
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * list转map
	 * 
	 * @param list
	 * @return
	 */
	private Map<String, Map<String, Object>> list2Map(List<Map<String, Object>> list) {
		Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
		for (Map<String, Object> m : list) {
			map.put(m.get("id") + "", m);
		}
		return map;
	}

	/**
	 * 从areaList中取出所有市
	 * 
	 * @param areaList
	 * @return
	 */
	private List<Map<String, Object>> findCity(List<Map<String, Object>> areaList) {
		List<Map<String, Object>> cityList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> area : areaList) {
			if (((Integer) area.get("area_level")) == 2)
				cityList.add(area);
		}
		return cityList;
	}

	/**
	 * 从areaList中取出所有区
	 * 
	 * @param areaList
	 * @return
	 */
	private List<Map<String, Object>> findArea(List<Map<String, Object>> areaList) {
		List<Map<String, Object>> cityList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> area : areaList) {
			if (((Integer) area.get("area_level")) == 3)
				cityList.add(area);
		}
		return cityList;
	}

}
